package com.spa.domain.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.domain.entity.OrderTransfer;
import com.spa.infrastructure.enums.dict.order.OrderTransferStatusEnum;
import com.spa.infrastructure.mapper.OrderTransferMapperStruct;
import com.spa.infrastructure.repository.condition.OrderTransferCondition;
import com.spa.infrastructure.repository.po.OrderTransferPO;
import com.spa.infrastructure.repository.service.OrderTransferRepositoryService;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单转单表(OrderTransfer)服务实现类
 *
 * @author makejava
 * @since 2024-01-31 11:38:17
 */
@Service
@Data
public class OrderTransferService {

    @Resource
    private OrderTransferRepositoryService orderTransferRepositoryService;

    public List<OrderTransfer> listByCondition(OrderTransferCondition condition) {
        return OrderTransferMapperStruct.INSTANCE.po2Entity4List(orderTransferRepositoryService.listInfo(condition));
    }

    public List<OrderTransfer> list4GroupByOrderLimitOne(OrderTransferCondition condition) {
        return OrderTransferMapperStruct.INSTANCE.po2Entity4List(orderTransferRepositoryService.list4GroupByOrderLimitOne(condition));
    }

    public Page<OrderTransfer> page(OrderTransferCondition condition) {
        condition.setDescCreateTime("desc");
        return OrderTransferMapperStruct.INSTANCE.po2Entity4Page(orderTransferRepositoryService.pageListInfo(condition));
    }

    public Page<OrderTransfer> page4GroupByOrderLimitOne(OrderTransferCondition condition) {
        condition.setDescCreateTime("desc");
        return OrderTransferMapperStruct.INSTANCE.po2Entity4Page(orderTransferRepositoryService.page4GroupByOrderLimitOne(condition));
    }

    /**
     * 只统计需要门店管理员处理的订单
     * @param shopId
     * @param statusEnum
     * @return
     */
    public Long countByShopId(Long shopId,OrderTransferStatusEnum statusEnum) {
        OrderTransferCondition condition = new OrderTransferCondition();
        condition.setShopId(shopId);
        condition.setStatus(statusEnum.getCode());
//        condition.setHandleMark(1);
        return orderTransferRepositoryService.list4GroupByOrderLimitOne(condition).stream().count();
    }

    public boolean add(OrderTransfer orderTransfer, Long createId, String createName, int handleMark) {
        OrderTransferPO orderTransferPO = OrderTransferMapperStruct.INSTANCE.po2Entity(orderTransfer);
        orderTransferPO.setHandleMark(handleMark);
        return orderTransferRepositoryService.add(orderTransferPO,createId,createName);
    }

    public boolean updateReceivByOrderId(OrderTransfer orderTransfer,  Long orderId, long mid) {
        OrderTransferCondition condition = new OrderTransferCondition();
        condition.setOrderId(orderId);
        condition.setStatus(OrderTransferStatusEnum.$10.getCode());
        condition.setAppointMassagistId(mid);
        return orderTransferRepositoryService.update(OrderTransferMapperStruct.INSTANCE.po2Entity(orderTransfer), condition);
    }

    public boolean deleteByOrderId(OrderTransfer orderTransfer,  Long orderId) {
        OrderTransferCondition condition = new OrderTransferCondition();
        condition.setOrderId(orderId);
        condition.setStatus(OrderTransferStatusEnum.$10.getCode());
        return orderTransferRepositoryService.update(OrderTransferMapperStruct.INSTANCE.po2Entity(orderTransfer), condition);
    }

    public boolean existsWaitByOrderId(Long orderId) {
        OrderTransferCondition condition = new OrderTransferCondition();
        condition.setOrderId(orderId);
        condition.setStatus(OrderTransferStatusEnum.$10.getCode());
        return orderTransferRepositoryService.exists(condition);
    }

    public boolean existsWaitByOrderId(Long orderId,OrderTransferStatusEnum statusEnum) {
        OrderTransferCondition condition = new OrderTransferCondition();
        condition.setOrderId(orderId);
        if (ObjectUtil.isNotNull(statusEnum)){
            condition.setStatus(statusEnum.getCode());
        }
        return orderTransferRepositoryService.exists(condition);
    }

    public OrderTransfer getTransferByOrderId(Long orderId) {
        OrderTransferCondition condition = new OrderTransferCondition();
        condition.setOrderId(orderId);
        return OrderTransferMapperStruct.INSTANCE.po2Entity(orderTransferRepositoryService.getOne(condition));
    }

}
